package com.Ajava1704.for并发包下的类.ForConditation;

/**
 * @author : LCG
 * 日期: 2020/6/22 23:57
 * 描述:
 */
public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {

        Object objectA=new Object();

        ThreadA threadA=new ThreadA(objectA);
        threadA.start();

        ThreadB threadB=new ThreadB(objectA);

        threadB.start();


        ThreadC threadC=new ThreadC(objectA);

        threadC.start();

        ThreadD threadD=new ThreadD(objectA);

        threadD.start();

        ThreadE threadE=new ThreadE(objectA);

        threadE.start();

        Thread.sleep(3000);

        ThreadUtils threadUtils=new ThreadUtils(objectA);

        threadUtils.noty();

    }


    static class ThreadA extends Thread{

        private Object testConditation;

        public ThreadA(Object testConditation) {
            this.testConditation = testConditation;
        }

        @Override
        public void run() {
            synchronized (testConditation){
                try {
                    System.out.println("====WAIT==========A");
                    testConditation.wait();
                    System.out.println("====RUN==========A");
                    synchronized (testConditation){
                        testConditation.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    static class ThreadB extends Thread{

        private Object testConditation;

        public ThreadB(Object testConditation) {
            this.testConditation = testConditation;
        }

        @Override
        public void run() {
            synchronized (testConditation){
                try {
                    System.out.println("====WAITB==========B");
                    testConditation.wait();
                    System.out.println("====RUNB==========B");
                    synchronized (testConditation){
                        testConditation.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    static class ThreadC extends Thread{

        private Object testConditation;

        public ThreadC(Object testConditation) {
            this.testConditation = testConditation;
        }

        @Override
        public void run() {
            synchronized (testConditation){
                try {
                    System.out.println("====WAIT==========C");
                    testConditation.wait();
                    System.out.println("====RUN==========C");
                    synchronized (testConditation){
                        testConditation.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    static class ThreadD extends Thread{

        private Object testConditation;

        public ThreadD(Object testConditation) {
            this.testConditation = testConditation;
        }

        @Override
        public void run() {
            synchronized (testConditation){
                try {
                    System.out.println("====WAIT==========D");
                    testConditation.wait();
                    System.out.println("====RUN==========D");
                    synchronized (testConditation){
                        testConditation.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    static class ThreadE extends Thread{

        private Object testConditation;

        public ThreadE(Object testConditation) {
            this.testConditation = testConditation;
        }

        @Override
        public void run() {
            synchronized (testConditation){
                try {
                    System.out.println("====WAIT==========E");
                    testConditation.wait();
                    System.out.println("====RUN==========E");
                    synchronized (testConditation){
                        testConditation.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
